package jsr166.contrib.uncontended;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

/**
 * This class decorates a ReadWriteLock , modifying it only by
 * adding a slight delay after either the read or write lock lock is released. This may be useful
 * if you think the code is correctly synchronized 
 * but may suffer from atomicity problems.
 * @author pugh
 *
 */
public class SlowReleasingReadWriteLock implements ReadWriteLock {

    /**
     * The wrapped lock delays for one millisecond after releasing a lock.
     * @param lock the lock to decorate/wrap
     */
    SlowReleasingReadWriteLock(ReadWriteLock lock) {
        this(lock, 1, TimeUnit.MILLISECONDS);
    }
    

    /**
     * The provided lock is wrapped so that any unlock delays by the specified amount
     * @param lock the lock to decorate/wrap
     * @param time the amount of time to delay after releasing a lock
     * @param units the time units
     */
    SlowReleasingReadWriteLock(ReadWriteLock lock, long time, TimeUnit units) {
        readLock = new SlowReleasingLock(lock.readLock(), time, units);
        writeLock = new SlowReleasingLock(lock.writeLock(), time, units);
    }
    final Lock readLock, writeLock;
    
    public Lock readLock() {
        return readLock;
    }

    public Lock writeLock() {
        return writeLock;
    }
    

}
